const cloud = require('wx-server-sdk');

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

const db = cloud.database().collection("timeTable");
const _ = cloud.database().command;

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  const startTimestamp = event.startTimestamp
  const endTimestamp = event.endTimestamp
  if(startTimestamp == null || endTimestamp == null)
  {
    return {
      code:400,
      data:{},
      msg:"empty param"
    }
  }
    const res = await db.where({timestamp:_.and(_.gt(startTimestamp),_.lt(endTimestamp))}).orderBy("timestamp","asc").limit(35).get()
    if(res.data.length == 0)
    {
      return {
        code:404,
        data:{},
        msg:"empty result"
      }
    }
    return {
      code:200,
      data:res.data,
      msg:"success",
      startTimestamp: startTimestamp,
      endTimestamp: endTimestamp
    }
};
